import {
  FieldMetadataType,
  RelationOnDeleteAction,
  RelationType,
} from 'twenty-shared/types';

import { type FlatFieldMetadata } from 'src/engine/metadata-modules/flat-field-metadata/types/flat-field-metadata.type';
import { type AllStandardObjectFieldName } from 'src/engine/workspace-manager/twenty-standard-application/types/all-standard-object-field-name.type';
import { createStandardFieldFlatMetadata } from 'src/engine/workspace-manager/twenty-standard-application/utils/create-standard-field-flat-metadata.util';
import { createStandardRelationFieldFlatMetadata } from 'src/engine/workspace-manager/twenty-standard-application/utils/create-standard-relation-field-flat-metadata.util';
import { type StandardFieldMetadataIdByObjectAndFieldName } from 'src/engine/workspace-manager/twenty-standard-application/utils/get-standard-field-metadata-id-by-object-and-field-name.util';

export const buildAttachmentStandardFlatFieldMetadatas = ({
  createdAt,
  workspaceId,
  standardFieldMetadataIdByObjectAndFieldName,
}: {
  createdAt: Date;
  workspaceId: string;
  standardFieldMetadataIdByObjectAndFieldName: StandardFieldMetadataIdByObjectAndFieldName;
}): Record<AllStandardObjectFieldName<'attachment'>, FlatFieldMetadata> => ({
  // Base fields from BaseWorkspaceEntity
  id: createStandardFieldFlatMetadata({
    objectName: 'attachment',
    workspaceId,
    options: {
      fieldName: 'id',
      type: FieldMetadataType.UUID,
      label: 'Id',
      description: 'Id',
      icon: 'Icon123',
      isSystem: true,
      isNullable: false,
      isUIReadOnly: true,
      defaultValue: 'uuid',
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  createdAt: createStandardFieldFlatMetadata({
    objectName: 'attachment',
    workspaceId,
    options: {
      fieldName: 'createdAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Creation date',
      description: 'Creation date',
      icon: 'IconCalendar',
      isNullable: false,
      isUIReadOnly: true,
      defaultValue: 'now',
      settings: {
        displayFormat: 'RELATIVE',
      },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  updatedAt: createStandardFieldFlatMetadata({
    objectName: 'attachment',
    workspaceId,
    options: {
      fieldName: 'updatedAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Last update',
      description: 'Last time the record was changed',
      icon: 'IconCalendarClock',
      isNullable: false,
      isUIReadOnly: true,
      defaultValue: 'now',
      settings: {
        displayFormat: 'RELATIVE',
      },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  deletedAt: createStandardFieldFlatMetadata({
    objectName: 'attachment',
    workspaceId,
    options: {
      fieldName: 'deletedAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Deleted at',
      description: 'Date when the record was deleted',
      icon: 'IconCalendarMinus',
      isNullable: true,
      isUIReadOnly: true,
      settings: {
        displayFormat: 'RELATIVE',
      },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),

  // Attachment-specific fields
  name: createStandardFieldFlatMetadata({
    objectName: 'attachment',
    workspaceId,
    options: {
      fieldName: 'name',
      type: FieldMetadataType.TEXT,
      label: 'Name',
      description: 'Attachment name',
      icon: 'IconFileUpload',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  fullPath: createStandardFieldFlatMetadata({
    objectName: 'attachment',
    workspaceId,
    options: {
      fieldName: 'fullPath',
      type: FieldMetadataType.TEXT,
      label: 'Full path',
      description: 'Attachment full path',
      icon: 'IconLink',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  type: createStandardFieldFlatMetadata({
    objectName: 'attachment',
    workspaceId,
    options: {
      fieldName: 'type',
      type: FieldMetadataType.TEXT,
      label: 'Type (deprecated)',
      description: 'Attachment type (deprecated - use fileCategory)',
      icon: 'IconList',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  fileCategory: createStandardFieldFlatMetadata({
    objectName: 'attachment',
    workspaceId,
    options: {
      fieldName: 'fileCategory',
      type: FieldMetadataType.SELECT,
      label: 'File category',
      description: 'Attachment file category',
      icon: 'IconList',
      isNullable: false,
      defaultValue: "'OTHER'",
      options: [
        { value: 'ARCHIVE', label: 'Archive', position: 0, color: 'gray' },
        { value: 'AUDIO', label: 'Audio', position: 1, color: 'pink' },
        { value: 'IMAGE', label: 'Image', position: 2, color: 'yellow' },
        {
          value: 'PRESENTATION',
          label: 'Presentation',
          position: 3,
          color: 'orange',
        },
        {
          value: 'SPREADSHEET',
          label: 'Spreadsheet',
          position: 4,
          color: 'turquoise',
        },
        {
          value: 'TEXT_DOCUMENT',
          label: 'Text Document',
          position: 5,
          color: 'blue',
        },
        { value: 'VIDEO', label: 'Video', position: 6, color: 'purple' },
        { value: 'OTHER', label: 'Other', position: 7, color: 'gray' },
      ],
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  createdBy: createStandardFieldFlatMetadata({
    objectName: 'attachment',
    workspaceId,
    options: {
      fieldName: 'createdBy',
      type: FieldMetadataType.ACTOR,
      label: 'Created by',
      description: 'The creator of the record',
      icon: 'IconCreativeCommonsSa',
      isUIReadOnly: true,
      isNullable: false,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),

  // Relation fields
  author: createStandardRelationFieldFlatMetadata({
    objectName: 'attachment',
    workspaceId,
    options: {
      fieldName: 'author',
      label: 'Author',
      description: 'Attachment author (deprecated - use createdBy)',
      icon: 'IconCircleUser',
      isNullable: true,
      createdAt,
      targetObjectName: 'workspaceMember',
      targetFieldName: 'authoredAttachments',
      settings: {
        relationType: RelationType.MANY_TO_ONE,
        onDelete: RelationOnDeleteAction.SET_NULL,
        joinColumnName: 'authorId',
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  activity: createStandardRelationFieldFlatMetadata({
    objectName: 'attachment',
    workspaceId,
    options: {
      fieldName: 'activity',
      label: 'Activity',
      description: 'Attachment activity',
      icon: 'IconNotes',
      isNullable: true,
      createdAt,
      // Activity is deprecated, pointing to a non-existent target
      targetObjectName: 'note',
      targetFieldName: 'attachments',
      settings: {
        relationType: RelationType.MANY_TO_ONE,
        onDelete: RelationOnDeleteAction.SET_NULL,
        joinColumnName: 'activityId',
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  task: createStandardRelationFieldFlatMetadata({
    objectName: 'attachment',
    workspaceId,
    options: {
      fieldName: 'task',
      label: 'Task',
      description: 'Attachment task',
      icon: 'IconNotes',
      isNullable: true,
      createdAt,
      targetObjectName: 'task',
      targetFieldName: 'attachments',
      settings: {
        relationType: RelationType.MANY_TO_ONE,
        onDelete: RelationOnDeleteAction.SET_NULL,
        joinColumnName: 'taskId',
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  note: createStandardRelationFieldFlatMetadata({
    objectName: 'attachment',
    workspaceId,
    options: {
      fieldName: 'note',
      label: 'Note',
      description: 'Attachment note',
      icon: 'IconNotes',
      isNullable: true,
      createdAt,
      targetObjectName: 'note',
      targetFieldName: 'attachments',
      settings: {
        relationType: RelationType.MANY_TO_ONE,
        onDelete: RelationOnDeleteAction.SET_NULL,
        joinColumnName: 'noteId',
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  person: createStandardRelationFieldFlatMetadata({
    objectName: 'attachment',
    workspaceId,
    options: {
      fieldName: 'person',
      label: 'Person',
      description: 'Attachment person',
      icon: 'IconUser',
      isNullable: true,
      createdAt,
      targetObjectName: 'person',
      targetFieldName: 'attachments',
      settings: {
        relationType: RelationType.MANY_TO_ONE,
        onDelete: RelationOnDeleteAction.CASCADE,
        joinColumnName: 'personId',
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  company: createStandardRelationFieldFlatMetadata({
    objectName: 'attachment',
    workspaceId,
    options: {
      fieldName: 'company',
      label: 'Company',
      description: 'Attachment company',
      icon: 'IconBuildingSkyscraper',
      isNullable: true,
      createdAt,
      targetObjectName: 'company',
      targetFieldName: 'attachments',
      settings: {
        relationType: RelationType.MANY_TO_ONE,
        onDelete: RelationOnDeleteAction.CASCADE,
        joinColumnName: 'companyId',
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  opportunity: createStandardRelationFieldFlatMetadata({
    objectName: 'attachment',
    workspaceId,
    options: {
      fieldName: 'opportunity',
      label: 'Opportunity',
      description: 'Attachment opportunity',
      icon: 'IconBuildingSkyscraper',
      isNullable: true,
      createdAt,
      targetObjectName: 'opportunity',
      targetFieldName: 'attachments',
      settings: {
        relationType: RelationType.MANY_TO_ONE,
        onDelete: RelationOnDeleteAction.CASCADE,
        joinColumnName: 'opportunityId',
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  dashboard: createStandardRelationFieldFlatMetadata({
    objectName: 'attachment',
    workspaceId,
    options: {
      fieldName: 'dashboard',
      label: 'Dashboard',
      description: 'Attachment dashboard',
      icon: 'IconLayout',
      isNullable: true,
      createdAt,
      targetObjectName: 'dashboard',
      targetFieldName: 'attachments',
      settings: {
        relationType: RelationType.MANY_TO_ONE,
        onDelete: RelationOnDeleteAction.CASCADE,
        joinColumnName: 'dashboardId',
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  workflow: createStandardRelationFieldFlatMetadata({
    objectName: 'attachment',
    workspaceId,
    options: {
      fieldName: 'workflow',
      label: 'Workflow',
      description: 'Attachment workflow',
      icon: 'IconSettingsAutomation',
      isNullable: true,
      createdAt,
      targetObjectName: 'workflow',
      targetFieldName: 'attachments',
      settings: {
        relationType: RelationType.MANY_TO_ONE,
        onDelete: RelationOnDeleteAction.CASCADE,
        joinColumnName: 'workflowId',
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  custom: createStandardRelationFieldFlatMetadata({
    objectName: 'attachment',
    workspaceId,
    options: {
      fieldName: 'custom',
      label: 'Custom',
      description: 'Attachment custom object',
      icon: 'IconBuildingSkyscraper',
      isNullable: true,
      createdAt,
      // Custom is a dynamic relation, pointing to custom objects
      targetObjectName: 'note',
      targetFieldName: 'attachments',
      settings: {
        relationType: RelationType.MANY_TO_ONE,
        onDelete: RelationOnDeleteAction.CASCADE,
        joinColumnName: 'customId',
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
});
